package com.yunwushanhe.student.util;

import com.alibaba.fastjson.JSON;
import com.yunwushanhe.student.Contants.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 这个类主要是处理请求被拦截后把通知信息发送给前端的情况
 */
@Slf4j
public class ResponseUtil {
    public static void responseJson(HttpServletResponse response,Object data){
        response.setCharacterEncoding(Constant.UTF_8_NAME);
        response.setContentType(Constant.APPLICATION_JSON_VALUE);
        try(PrintWriter pw = response.getWriter()){
            pw.print(JSON.toJSONString(data)); // 请求被拦截后需要通知前端的信息
            pw.flush();
        } catch (IOException e){
            log.error("写道前端的异常: ",e);
        }
    }
}
